package org.example.leetCode;

import java.util.LinkedList;

/**
 * @author Administrator
 */
public class AddTwoNumbers {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(0);
        ListNode root = res;
        boolean ten = false;
        while (l1 != null || l2 != null || ten) {
            l1 = l1 == null ? new ListNode(0) : l1;
            l2 = l2 == null ? new ListNode(0) : l2;
            ListNode val = new ListNode(l1.val + l2.val);
            if (ten) {
                val.val++;
                ten = false;
            }
            if (val.val >= 10) {
                val.val = val.val % 10;
                ten = true;
            }
            res.next = val;
            res = res.next;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        return root.next;
    }
}


